package com.deyuanyun.pic.settings.controller.commonauthentication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.UserSessionVO;
import com.deyuanyun.pic.settings.domain.base.Threshold;
import com.deyuanyun.pic.settings.domain.prvlg.Menu;
import com.deyuanyun.pic.settings.service.prvlg.MenuService;
import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;

/**
 * 获取菜单的controller，不要加settings前缀
 */

@Controller
@RequestMapping("/menu")
public class MenuAuthenticationController {

	@Resource
	private MenuService menuService;

    @RequestMapping("/toMenu")
    public String toMenu() {
        return "/base/menuT";
    }


	/**
	 * 获取左侧的大菜单
	 * @param model
	 * @param request
	 * @param menuMark
	 * @throws Exception
     */
	@RequestMapping("/menuList.json")
	@ResponseBody
	public void queryMenuList(Model model, HttpServletRequest request,String menuMark) throws Exception{
		UserSessionVO sessionVO = SessionCacheSupport.getSessionVO();
		List<Menu> menuList=null;
		if (StringUtils.isBlank(menuMark)){
			menuList=sessionVO.getTopMenu();
		}else {
			menuList=sessionVO.getMenusByParentId(menuMark);
		}
		List<Menu> menuList_1 = new ArrayList<Menu>();
		for (Menu menu : menuList) {
		    //移除type=1为功能的菜单点。
            if(menu != null && menu.getType() != null && menu.getType() == 1){
                
            }else{
                menuList_1.add(menu);
            }
        }
		Asserts.assertTrue(!ObjectUtil.isEmptyAll(menuList_1),"你还没配角色权限!");
		//Set<Menu> menuSet = new LinkedHashSet<Menu>();
		//menuSet.addAll(menuList_1);
		AjaxSupport.sendSuccessText("",menuList_1);

	}

	/**
	 * 阀值组名称列表
	 */
	@RequestMapping("/queryThresholdGroupList.json")
	@ResponseBody
	public void queryThresholdGroupList(){
		List<Threshold> thresholdList =menuService.queryThresholdGroupList(new HashMap());
		AjaxSupport.sendSuccessText("success", thresholdList);
		return;
	}



}
